package com.yizhuoyan.his.webapi.controller.admin;

import com.yizhuoyan.his.ao.ModifyUserAo;
import com.yizhuoyan.his.ao.UserAddAo;
import com.yizhuoyan.his.dto.JSONResponse;
import com.yizhuoyan.his.dto.QueryResult;
import com.yizhuoyan.his.entity.SysAccountEntity;
import com.yizhuoyan.his.service.admin.SysAccountManageService;
import com.yizhuoyan.his.webapi.controller.admin.vo.SysAccountVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class SysAccountManageController {
    @Autowired
    SysAccountManageService service;

    @PostMapping("/api/sys-user/")
    public ResponseEntity<JSONResponse> addSysUser(UserAddAo ao) throws Exception {
        SysAccountEntity e = service.addUser(ao);
        return ResponseEntity.ok().build();
    }
    @PutMapping("/api/sys-user/{id}")
    public ResponseEntity<Void> modifySysUser(@PathVariable Long id , ModifyUserAo ao) throws Exception {
        service.modifyUser(id,ao);
        return ResponseEntity.ok().build();
    }
    @DeleteMapping("/api/sys-user/{id}")
    public ResponseEntity<Void> removeSysUser(@PathVariable Long id ) throws Exception {
        service.removeUser(id);
        return ResponseEntity.ok().build();
    }
    @GetMapping("/api/sys-user/{id}")
    public ResponseEntity<SysAccountVo> querySysUser(@PathVariable Long id ) throws Exception {
        SysAccountEntity e = service.queryUser(id);
        return ResponseEntity.ok(SysAccountVo.of(e));
    }
    @GetMapping("/api/sys-user/")
    public JSONResponse list(String key, String pageNo, String pageSize) throws Exception {
        if (pageNo == null)
            pageNo = "1";
        if (pageSize == null)
            pageSize = "10";
        Integer pageNoInt=Integer.parseInt(pageNo);
        Integer pageSizeInt=Integer.parseInt(pageSize);
        QueryResult<SysAccountEntity> queryResult = service.queryUserByKey(key, pageNoInt, pageSizeInt);
        QueryResult<SysAccountVo> queryVoResult = queryResult.rowsMap(SysAccountVo::of);
        return JSONResponse.ok(queryVoResult);
    }

}
